﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Linq.Expressions;

using FineUIPro;
using CanDoo.FineUI;
using CanDoo.Oil.Core.Contracts;

namespace CanDoo.Oil.WebApp.sysConfig
{
    public partial class Set : PageBase
    {
        #region PagePower

        /// <summary>
        /// 本页面的浏览权限，空字符串表示本页面不受权限控制
        /// </summary>
        public override string PageCode
        {
            get
            {
                return "sysConfig";
            }
        }

        /// <summary>
        /// 本页面的浏览权限，空字符串表示本页面不受权限控制
        /// </summary>
        public override string ViewPower
        {
            get
            {
                return "sysConfigSet";
            }
        }

        /// <summary>
        /// 本页面的功能模块
        /// </summary>
        public override string PageTitle
        {
            get
            {
                return "系统设置";
            }
        }

        #endregion

        public IOilSiteConfig sysStaticConfig { get; set; }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                LoadData();
            }
        }

        private void LoadData()
        {
            ed_PAD_DOS.SetValue(sysStaticConfig.PAD_DOS);
            ed_PAD_VERSION.SetValue(sysStaticConfig.PAD_VERSION);
            ed_PAD_URL.SetValue(sysStaticConfig.PAD_URL);
            ed_PAD_NAME.SetValue(sysStaticConfig.PAD_NAME);
            ed_PAD_DES.SetValue(sysStaticConfig.PAD_DES);
            ed_PAD_UPDATETAB.SetValue(sysStaticConfig.PAD_UPDATETAB);
            ed_MEDIA_DOS.SetValue(sysStaticConfig.MEDIA_DOS);
            ed_MEDIA_VERSION.SetValue(sysStaticConfig.MEDIA_VERSION);
            ed_MEDIA_URL.SetValue(sysStaticConfig.MEDIA_URL);
            ed_MEDIA_NAME.SetValue(sysStaticConfig.MEDIA_NAME);
            ed_MEDIA_DES.SetValue(sysStaticConfig.MEDIA_DES);
            ed_MEDIA_UPDATETAB.SetValue(sysStaticConfig.MEDIA_UPDATETAB);
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            sysStaticConfig.SetValue("PAD_DOS", ed_PAD_DOS.GetValue());
            sysStaticConfig.SetValue("PAD_VERSION", ed_PAD_VERSION.GetValue());
            sysStaticConfig.SetValue("PAD_URL", ed_PAD_URL.GetValue());
            sysStaticConfig.SetValue("PAD_NAME", ed_PAD_NAME.GetValue());
            sysStaticConfig.SetValue("PAD_DES", ed_PAD_DES.GetValue());
            sysStaticConfig.SetValue("PAD_UPDATETAB", ed_PAD_UPDATETAB.GetValue());
            sysStaticConfig.SetValue("MEDIA_DOS", ed_MEDIA_DOS.GetValue());
            sysStaticConfig.SetValue("MEDIA_VERSION", ed_MEDIA_VERSION.GetValue());
            sysStaticConfig.SetValue("MEDIA_URL", ed_MEDIA_URL.GetValue());
            sysStaticConfig.SetValue("MEDIA_NAME", ed_MEDIA_NAME.GetValue());
            sysStaticConfig.SetValue("MEDIA_DES", ed_MEDIA_DES.GetValue());
            sysStaticConfig.SetValue("MEDIA_UPDATETAB", ed_MEDIA_UPDATETAB.GetValue());

            LoadData();
            FineUIPro.Alert.ShowInTop("OK");

        }

        protected void fileLoginBackground_FileSelected(object sender, EventArgs e)
        {
            if (fileLoginBackground.HasFile)
            {
                string fileName = fileLoginBackground.ShortFileName;
                if (!ValidateFileType(fileName))
                {
                    Alert.ShowInTop("无效的文件类型！");
                    return;
                }


                ed_PAD_NAME.Text = fileName;

                var mUrl = UploadFile.Upload(fileLoginBackground, "apk");

                this.ed_PAD_URL.Text = mUrl;

                // 清空文件上传组件
                fileLoginBackground.Reset();
            }
        }

        protected void fileQrCodeBackground_FileSelected(object sender, EventArgs e)
        {
            if (fileQrCodeBackground.HasFile)
            {
                string fileName = fileQrCodeBackground.ShortFileName;


                if (!ValidateFileType(fileName))
                {
                    Alert.ShowInTop("无效的文件类型！");
                    return;
                }

                ed_MEDIA_NAME.Text = fileName;

                var mUrl = UploadFile.Upload(fileQrCodeBackground, "apk");
                this.ed_MEDIA_URL.Text = mUrl;

                // 清空文件上传组件
                fileQrCodeBackground.Reset();
            }
        }

        private bool ValidateFileType(string fileName)
        {
            return fileName.EndsWith(".apk");
        }
    }
}